/**
 *
 */
package com.zhuoyue.elmt.service;

import java.util.HashMap;
import java.util.List;

import com.eit.eitsite.common.utils.StringUtils;
import com.eit.eitsite.modules.sys.entity.Role;
import com.eit.eitsite.modules.sys.utils.UserUtils;
import com.google.common.collect.Lists;
import com.zhuoyue.elmt.dao.DeviceParametersDao;
import com.zhuoyue.elmt.dao.XtPqxqgxDao;
import com.zhuoyue.elmt.entity.DeviceParameters;
import com.zhuoyue.elmt.entity.WbMtGroupStaff;
import com.zhuoyue.elmt.entity.XtPqxqgx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.service.CrudService;
import com.zhuoyue.elmt.entity.XtPq;
import com.zhuoyue.elmt.dao.XtPqDao;

/**
 * 设备分组Service
 *
 * @author JiangYH
 * @version 2017-02-09
 */
@Service
@Transactional(readOnly = true)
public class XtPqService extends CrudService<XtPqDao, XtPq> {

    @Autowired
    XtPqxqgxDao xtPqxqgxDao;
    @Autowired
    DeviceParametersDao deviceParametersDao;

    public XtPq get(String id) {
        return super.get(id);
    }

    public List<XtPq> findList(XtPq xtPq) {
        return super.findList(xtPq);
    }

    public Page<XtPq> findPage(Page<XtPq> page, XtPq xtPq) {
        return super.findPage(page, xtPq);
    }

    public List<HashMap> findWbList(HashMap map) {
        return dao.findWbList(map);
    }

    @Transactional(readOnly = false)
    public void save(XtPq xtPq) {
        super.save(xtPq);
    }

    @Transactional(readOnly = false)
    public void updateStaffDevice(HashMap map) {
       dao.updateStaffDevice(map);
    }

    @Transactional(readOnly = false)
    public void savePq(XtPq xtPq) {
        if (StringUtils.isBlank(xtPq.getId())) {
            xtPq.preInsert();
            dao.insert(xtPq);
        } else {
            xtPq.preUpdate();
            dao.update(xtPq);
        }
        // 更新关联
        //dao.deletepqxqgx(xtPq);
        if ("1".equals(xtPq.getType())) {
            List staffList = Lists.newArrayList();
            if ("1".equals(xtPq.getChooseType())) {
                for (XtPqxqgx staffXtPqxqgx : xtPq.getStaffList()) {
                    for (XtPqxqgx xqXtPqxqgx : xtPq.getXqList()) {
                        XtPqxqgx xtPqxqgx = new XtPqxqgx();
                        xtPqxqgx.setStaffId(staffXtPqxqgx.getStaffId());
                        xtPqxqgx.setXqId(xqXtPqxqgx.getXqId());
                        staffList.add(xtPqxqgx);
                    }
                }
                xtPq.setStaffList(staffList);
                dao.insertPqStaffXq(xtPq);
            } else if ("2".equals(xtPq.getChooseType())) {
                for (XtPqxqgx staffXtPqxqgx : xtPq.getStaffList()) {
                    if (xtPq.getSbList().size() > 0) {
                        for (XtPqxqgx xqXtPqxqgx : xtPq.getSbList()) {
                            XtPqxqgx xtPqxqgx = new XtPqxqgx();
                            xtPqxqgx.setStaffId(staffXtPqxqgx.getStaffId());
                            xtPqxqgx.setSbId(xqXtPqxqgx.getSbId());
                            staffList.add(xtPqxqgx);
                        }
                    } else {
                        XtPqxqgx xtPqxqgx = new XtPqxqgx();
                        xtPqxqgx.setStaffId(staffXtPqxqgx.getStaffId());
                        xtPqxqgx.setSbId("0");
                        staffList.add(xtPqxqgx);
                    }
                }
                xtPq.setStaffList(staffList);
                dao.insertPqStaffSb(xtPq);
            }
        } else if ("2".equals(xtPq.getType())) {
            List groupList = Lists.newArrayList();
            if ("1".equals(xtPq.getChooseType())) {
                for (XtPqxqgx groupXtPqxqgx : xtPq.getGroupList()) {
                    for (XtPqxqgx xqXtPqxqgx : xtPq.getXqList()) {
                        XtPqxqgx xtPqxqgx = new XtPqxqgx();
                        xtPqxqgx.setGroupId(groupXtPqxqgx.getGroupId());
                        xtPqxqgx.setXqId(xqXtPqxqgx.getXqId());
                        groupList.add(xtPqxqgx);
                    }
                }
                xtPq.setGroupList(groupList);
                dao.insertPqGroupXq(xtPq);
            } else if ("2".equals(xtPq.getChooseType())) {
                for (XtPqxqgx groupXtPqxqgx : xtPq.getGroupList()) {
                    if (xtPq.getSbList().size() > 0) {
                        for (XtPqxqgx xqXtPqxqgx : xtPq.getSbList()) {
                            XtPqxqgx xtPqxqgx = new XtPqxqgx();
                            xtPqxqgx.setGroupId(groupXtPqxqgx.getGroupId());
                            xtPqxqgx.setSbId(xqXtPqxqgx.getSbId());
                            groupList.add(xtPqxqgx);
                        }
                    } else {
                        XtPqxqgx xtPqxqgx = new XtPqxqgx();
                        xtPqxqgx.setGroupId(groupXtPqxqgx.getGroupId());
                        xtPqxqgx.setSbId("0");
                        groupList.add(xtPqxqgx);
                    }
                }
                xtPq.setGroupList(groupList);
                dao.insertPqGroupSb(xtPq);
            }
        }
        HashMap paramMap = new HashMap();
        paramMap.put("in_pq_id", xtPq.getId());
        paramMap.put("in_wbdwdm", xtPq.getMaintenUnitCode());
        paramMap.put("in_delete_pq_id", "0");
        dao.updateStaffDevice(paramMap);
    }

    /**
     * 按区域分配设备给小组
     * @param xtPq
     */
    @Transactional(readOnly = false)
    public void xtPqXqSave(XtPq xtPq) {

        XtPqxqgx xtPqxqgx;
            // 按选择区域插入记录
        if(xtPq.getXqList().size() > 0) {

            //先删除全部区域的
            xtPqxqgx=new XtPqxqgx();
            xtPqxqgx.setPqId(xtPq.getPqId());
            xtPqxqgx.setXqId("1");
            dao.deletepqxqgx(xtPqxqgx);

            for (XtPqxqgx _xqXtPqxqgx : xtPq.getXqList()) {
                HashMap qryMap = new HashMap();
                qryMap.put("pqId", xtPq.getPqId());
                qryMap.put("wbdwdm", UserUtils.getUser().getCompany().getCode());
                qryMap.put("sbsyqy",_xqXtPqxqgx.getXqId());
                qryMap.put("groupId",  xtPq.getPqId());
                qryMap.put("tzsbbh", null);

                xtPqxqgx=new XtPqxqgx();
                xtPqxqgx.setXqId(_xqXtPqxqgx.getXqId());
                xtPqxqgx.setPqId(xtPq.getPqId());
                xtPqxqgx.setGroupId(xtPq.getPqId());
                //xtPqxqgx.setSbId(_deviceParametersList.getTzsbbh());

                //在新增
                dao.insertPqSb(xtPqxqgx);

                /**
                List<DeviceParameters> deviceParametersList= deviceParametersDao.xtpqsbnolist(qryMap);

                for (DeviceParameters _deviceParametersList : deviceParametersList) {
                    XtPqxqgx xtPqxqgx=new XtPqxqgx();
                    xtPqxqgx.setXqId(_xqXtPqxqgx.getXqId());
                    xtPqxqgx.setPqId(xtPq.getPqId());
                    xtPqxqgx.setGroupId(xtPq.getPqId());
                    xtPqxqgx.setSbId(_deviceParametersList.getTzsbbh());
                    dao.insertPqSb(xtPqxqgx);
                }
                */
            }
        }
        // 按选择设备插入记录
        if(xtPq.getSbList().size() > 0){
            for (XtPqxqgx _xqXtPqxqgx : xtPq.getSbList()) {
                 xtPqxqgx=new XtPqxqgx();
                xtPqxqgx.setSbId(_xqXtPqxqgx.getSbId());
                xtPqxqgx.setPqId(xtPq.getPqId());
                xtPqxqgx.setGroupId(xtPq.getPqId());
                dao.insertPqSb(xtPqxqgx);
            }
        }
        //按选择合同设备插入记录



    }
    @Transactional(readOnly = false)
    public void delete(XtPq xtPq) {
        XtPqxqgx qryXtPqxqgx = new XtPqxqgx();
        qryXtPqxqgx.setPqId(xtPq.getId());

        HashMap paramMap = new HashMap();
        paramMap.put("in_pq_id", xtPq.getId());
        paramMap.put("in_wbdwdm", xtPq.getMaintenUnitCode());
        paramMap.put("in_delete_pq_id", xtPq.getId());
        dao.updateStaffDevice(paramMap);

        xtPqxqgxDao.deleterela(qryXtPqxqgx);
        super.delete(xtPq);
    }

}